package servlet;

import entity.Phone;
import service.PhoneService;
import service.impl.PhoneServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;

@WebServlet("/addPhone.do")
public class AddPhoneServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        double price = Double.parseDouble(req.getParameter("price"));
        int inventory = Integer.parseInt(req.getParameter("inventory"));
        String type = req.getParameter("type");
        Date date = Date.valueOf(req.getParameter("date"));
        PhoneService phoneService = new PhoneServiceImpl();
        try{
            phoneService.addPhone(new Phone(inventory,name,type,price,date));
            System.out.println("添加成功！");
            resp.sendRedirect("/findAllPhones.do");
        }catch (SQLException e){
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
